<?php
/*
 * Copyright (c) 2008 Sergey Bondari, bondari@bondari.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Mosaic\LIB;


/**
 * RDF interface wrapper
 *
 * @package Library
 */
abstract class aclRDF {
	abstract function loadFile($filename) ;
	abstract function loadXML($xml) ;
	abstract function find($subject = false, $predicate = false, $object = false, $resolveContainers = false);

	protected function checkFile($filename) {
		if (!file_exists($filename))
			throw new \Mosaic\System\IOException($filename.' not found');
	}
}


define("RDFAPI_INCLUDE_DIR", dirname(__FILE__)."/rdfapi/");

include(RDFAPI_INCLUDE_DIR . "RdfAPI.php");

/**
 * RDF Literal wrapper
 *
 * @package Library
 */
 class RDFLiteral extends \Literal { } 

 /**
  * RDF Resource wrapper
  *
  * @package Library
  */
class RDFResource extends \Resource { } 

/**
 * RDF Statement wrapper
 *
 * @package Library
 */
class RDFStatement extends \Statement { 
	public function getLabelPredicate() {
		return parent::getPredicateLabel();
	}

	public function getLabelSubject() {
		return parent::getSubjectLabel();
	}
	
	public function getLabelObject() {
		return parent::getObjectLabel();
	}
}

/**
 * RDF parser wrapper
 * @package Library
*/
class RDF extends aclRDF {
	public $rdf;


	function __construct() {
		$this->rdf = \ModelFactory::getDefaultModel();
	}

	public function loadFile($filename) {
		$this->checkFile($filename);
		$this->rdf = \ModelFactory::getDefaultModel();
		return $this->rdf->load($filename);
        }

	public function loadXML($xml) {
		$this->rdf = \ModelFactory::getDefaultModel();
		return $this->rdf->load($xml);
        }

	public function findFirst($subject = false, $predicate = false, $object = false, $fw = false) {
		$ret = $this->find($subject, $predicate, $object, $fw);
		if (count($ret)) return array_shift($ret); else return false;
        }


	public function find($subject = false, $predicate = false, $object = false, $resolveContainers = false) {
		$result = $resolveContainers ? $this->rdf->findForward($subject, $predicate, $object) : $this->rdf->find($subject, $predicate, $object);
		$ret = Array();
		$it = $result->getStatementIterator();
		while ($it->hasNext()) {
			$statement = $it->next();
			$ret[] = $statement;
		}
		return $ret;

	}


	public function saveToString() {
		return $this->rdf->writeRdfToString();
	}

	public function unite(RDF $rdf) {
		$this->rdf->addModel($rdf->rdf);
	}
	
}
